using System.Collections.Generic;
using Assignment_2.Entities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Assignment_2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        private RasterizerState _wireframeRasterizerState;
        private readonly GraphicsDeviceManager _graphics;

        private BasicEffect _effect;

        private readonly FPS _fps;
        public Camera Camera { get; set; }
        private Input _input;

        public Dictionary<string, Entity> Entities { get; set; }
        private readonly Matrix _world = Matrix.Identity;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this)
                {
                    PreferredBackBufferHeight = 900,
                    PreferredBackBufferWidth = 1280,
                    PreferMultiSampling = false
                };
            Content.RootDirectory = "Content";
#if DEBUG
            _fps = new FPS(this);
#else
            fps = new FPS(this,true,true,this.TargetElapsedTime);
#endif
            Components.Add(_fps);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Entities = new Dictionary<string, Entity>();
            _wireframeRasterizerState = new RasterizerState
                {
                    CullMode = CullMode.CullCounterClockwiseFace,
                    FillMode = FillMode.Solid
                };

            Camera = new Camera(new Vector3(0f, 0f, 0f));
            Camera.InitializeCamera(new Vector3(20f, 0f, 0f), GraphicsDevice.DisplayMode.AspectRatio);

            _input = new Input(this);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {

            GraphicsDevice.RasterizerState = _wireframeRasterizerState;

            _effect = new BasicEffect(_graphics.GraphicsDevice)
                {
                    VertexColorEnabled = false,
                    Projection = Camera.Projection,
                    World = _world
                };

            // Entity Loading //

            Entities.Add(EntityNames.World, new World(GraphicsDevice, Content.Load<Texture2D>("world_flat_ocean"), Content.Load<Texture2D>("world_topo")) { Name = EntityNames.World });
            Entities.Add(EntityNames.Ocean, new Ocean(GraphicsDevice, Content.Load<Texture2D>("world_flat_ocean"), Content.Load<Texture2D>("ocean")) { Name = EntityNames.Ocean, Scale = 1.001f });

            Entities.Add(EntityNames.Typhoon, new Typhoon(GraphicsDevice, this, Content.Load<Model>("typhoon")) {Name = EntityNames.Typhoon, Position = new Vector3(100f, 0f, -15f)});
            Camera.InitializeCamera(new Vector3(20f, 0f, 0f), GraphicsDevice.DisplayMode.AspectRatio);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            _input.Update(gameTime);

            foreach (var e in Entities.Values)
            {
                e.Update(gameTime, _world);
            }

            Camera.Update();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            foreach (var e in Entities.Values)
            {
                e.Draw(_effect);
            }

            Camera.Draw(_effect);

            base.Draw(gameTime);
        }
    }
}
